//
//  HHAlertControllerAnimated.m
//  XueZhiYun
//
//  Created by Basic. on 2019/8/21.
//  Copyright © 2019 Zhihan. All rights reserved.
//

#import "HHAlertControllerAnimated.h"

@implementation HHAlertControllerAnimated

-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.35f;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    if (!fromVC || !toVC) {
        return;
    }
    UIView *containerView = [transitionContext containerView];
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    if (toVC.isBeingPresented) {
        [containerView addSubview:toVC.view];
        UIView *alertView = nil;
        if ([toVC conformsToProtocol:@protocol(HHAlertAnimatedController)]){
            UIAlertController<HHAlertAnimatedController> *alertVC = (UIAlertController<HHAlertAnimatedController> *)toVC;
            if ([alertVC respondsToSelector:@selector(alertView)]) {
                alertView = alertVC.alertView;
            }
            if (!alertView && [alertVC respondsToSelector:@selector(getAlertView)]) {
                alertView = [alertVC getAlertView];
            }
        } else {
            alertView = toVC.view;
        }
        if (!alertView) {
            return;
        }
        if (self.animStyel == HHAlertControlAnimatedStyel_Transform) {
            [self popAnimationWithView:alertView transition:transitionContext];
        }else{
            [self alphaAnimationWithView:alertView transition:transitionContext];
        }
    }
    else if (fromVC.isBeingDismissed) {
        [UIView animateWithDuration:duration animations:^{
            fromVC.view.alpha = 0.0;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
}

///MARK: -animation
- (void)popAnimationWithView:(UIView *)view transition:(id<UIViewControllerContextTransitioning>)transitionContext {
    
    CAKeyframeAnimation *popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    popAnimation.duration = 0.4;
    popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
                            [NSValue valueWithCATransform3D:CATransform3DIdentity]];
    popAnimation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
                                     [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    

    [view.layer addAnimation:popAnimation forKey:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    });
}
- (void)alphaAnimationWithView:(UIView *)view transition:(id<UIViewControllerContextTransitioning>)transitionContext {
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    view.alpha = 0;
    [UIView animateWithDuration:duration animations:^{
        view.alpha = 1.0f;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
}
@end
